<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="题库目录" prop="categoryId">
              <tree-select
                ref="categoryTreeSelectRef"
                v-model="dataForm.categoryId"
                :options="categoryIdOptions"
                :props="categoryIdProps"
                lastLevel
                :isChangeFather="false"
                placeholder="请选择"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="题库名称" prop="fullName">
              <el-input
                v-model="dataForm.fullName"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" @click="visible = false">
        取 消</el-button
      >
      <el-button :loading="btnLoading" type="primary" @click="dataFormSubmit()">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { getInfo, Update, Create } from "@/views/exam/api/questionLibrary";

export default {
  components: {},
  props: {
    categoryIdOptions: {
      type: Array,
      default: [],
    },
    categoryIdProps: {
      type: Object,
      default: {
        children: "children",
        label: "fullName",
        value: "id",
      },
    },
  },
  data() {
    return {
      isAdministrator: false,
      companyId: "",
      menuId: "",
      visible: false,
      loading: false,
      btnLoading: false,
      dataForm: {
        categoryId: "",
        enCode: "",
        fullName: "",
        description: "",
        enabledMark: 1,
        deleteMark: 0,
      },
      rules: {
        categoryId: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        enCode: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        fullName: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.isAdministrator = this.$store.getters.userInfo.isAdministrator;
    this.companyId = this.$store.getters.userInfo.organizeId;
  },
  mounted() {},
  methods: {
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, menuId) {
      this.menuId = menuId || "";
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          getInfo(this.dataForm.id).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
          this.dataForm.enabledMark = 1;
          this.dataForm.deleteMark = 0;
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          var _data = this.dataList();
          this.btnLoading = true;
          const formMethod = this.dataForm.id ? Update : Create;
          formMethod(_data)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refresh", true);
                },
              });
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.enabledMark = parseInt(_data.enabledMark);
      _data.deleteMark = parseInt(_data.deleteMark);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.enabledMark = parseInt(_dataAll.enabledMark);
      _dataAll.deleteMark = parseInt(_dataAll.deleteMark);
      this.dataForm = _dataAll;
    },
  },
};
</script>
